<?php
    /**
     * Created by PhpStorm.
     * User: timeteam
     * Date: 18-12-17
     * Time: 下午1:20
     */

    namespace app\common\behavior;


    class Request
    {
        public function run()
        {
            $dispatch = request()->path();

            $url = explode('/', strtolower($dispatch));

            $url_[] = isset($url[0]) ? $url[0] : 'index';

            $url_[] = isset($url[1]) ? $url[1] : 'index';

            $url_[] .= isset($url[2]) ? $url[2] : 'index';

            if ($dispatch) {
                $module = explode('/', $dispatch)[0];
            } else {
                $module = 'index';
            }


            is_swoole()? $this->swoole_run($module, $url_):  $this->phpfpm_run($module, $url_);
//            switch (config('run_server')) {
//                case 'swoole':
//                    $this->swoole_run($module, $url_);
//                    break;
//                case 'phpfpm':
//                    $this->phpfpm_run($module, $url_);
//                    break;
//                default:
//                    $this->phpfpm_run($module, $url_);
//            }
        }


        /**
         * phgfpm执行方式
         *
         * @param $module
         * @param $url_
         */
        public function phpfpm_run($module, $url_)
        {
            // 插件静态资源目录
            config('tpl_replace_string.__PLUGINS__', '/plugins');

            if (request()->baseFile() == '/api.php') {
                config('default_return_type', 'json');
                \think\facade\Env::set('ENTRANCE', 'api');
                if (!in_array($module, config('module.default_controller_layer'))) {
                    $v = '';
                    if (config('api_version')) {
                        $v = request()->header('api-version');
                        ($v == null) ? $v = "\\v1" : $v = "\\" . $v;
                    }
                    config('url_controller_layer', 'api' . $v);
                }

            } elseif (request()->baseFile() == '/admin.php') {

                request()->setModule($url_[0]);
                request()->setController($url_[1]);

                config('default_return_type', 'html');
                \think\facade\Env::set('ENTRANCE', 'admin');

                if (!in_array($module, config('module.default_controller_layer'))) {

                    config('url_controller_layer', 'admin');
                    // 修改视图模板路径
                    config('template.view_path', \think\facade\Env::get('app_path') . $module . '/view/admin/');
                }
            } else {
                if ($module == 'admin') {
                    header("Location: /" . $module . '.php/admin/index/index', true, 302);
                }
                config('default_return_type', 'html');
                \think\facade\Env::set('ENTRANCE', 'index');
                if (!in_array($module, config('module.default_controller_layer'))) {
                    // 修改默认访问控制器层
                    config('url_controller_layer', 'home');
                }
            }
        }

        /**
         * swoole执行方式
         *
         * @param $module
         * @param $url_
         */
        public function swoole_run($module, $url_)
        {
            // 插件静态资源目录
            config('tpl_replace_string.__PLUGINS__', '/plugins');
            $auth_request = request()->header('x-auth-request');
            if ($auth_request == 'api') {

                config('default_return_type', 'json');
                \think\facade\Env::set('ENTRANCE', 'api');
                if (!in_array($module, config('module.default_controller_layer'))) {
                    $v = '';
                    if (config('api_version')) {
                        $v = request()->header('api-version');
                        ($v == null) ? $v = "\\v1" : $v = "\\" . $v;
                    }
                    config('url_controller_layer', 'api' . $v);
                }

            } elseif ($auth_request == 'admin') {

                request()->setModule($url_[0]);
                request()->setController($url_[1]);

                config('default_return_type', 'html');
                \think\facade\Env::set('ENTRANCE', 'admin');

                if (!in_array($module, config('module.default_controller_layer'))) {

                    config('url_controller_layer', 'admin');
                    // 修改视图模板路径
                    config('template.view_path', \think\facade\Env::get('app_path') . $module . '/view/admin/');
                }

            } else {
                if ($module == 'admin') {
                    header("Location: /" . $module . '.php/admin/index/index', true, 302);
                }
                config('default_return_type', 'html');
                \think\facade\Env::set('ENTRANCE', 'index');
                if (!in_array($module, config('module.default_controller_layer'))) {
                    // 修改默认访问控制器层
                    config('url_controller_layer', 'home');
                }
            }
        }
    }